package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.autodata.specialserver.IceServerUserData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.specialserver.IceServerModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.iceservergift.IceServerGiftMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

public class IceServerGiftMallLimit extends BaseActivityMallLimit {

    public IceServerGiftMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo, activityInfo);
    }

    @Override
    protected int activityCheck() {
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if (activityInfo.getType() != eActivityType.IceServerGift.getValue()) {
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }

        return 0;
    }

    @Override
    protected boolean isSystemUnLock() {
        return SystemOpenMgr.systemOpen(player, eSystemId.IceServerGift.getValue());
    }

    @Override
    protected LimitBuyResult buyCountLimit() {
        return activityMallCountLimit(player);
    }

    @Override
    protected int logicCheck() {
        // 判断寒冬任务时间点
        String[] extendParam = mallInfo.getExtendParam().split("\\|");
        int taskId = Integer.parseInt(extendParam[4]);
        IceServerUserData userData = player.getModule(IceServerModule.class).getUserData();
        if (userData == null) {
            return GameErrorCode.E_ERROR_DATA;
        }
        if (!userData.getTaskCompleteTimeMap().containsKey(taskId)) {
            return GameErrorCode.E_ERROR_DATA;
        }
        long finishTime = userData.getTaskCompleteTimeMap().get(taskId);
        if (finishTime == 0) {
            return GameErrorCode.E_ERROR_DATA;
        }
        // 判断是否在时间内
        long nowTime = System.currentTimeMillis();
        if (nowTime < finishTime || nowTime > finishTime + IceServerGiftMgr.getGiftLastTime() * DateHelper.SECOND_MILLIONS) {
            return GameErrorCode.E_ERROR_DATA;
        }
        return player.getModule(ActivityMallModule.class).checkLinearPackLocked(mallInfo);
    }
}
